//
//  XBTabBar.m
//  微博
//
//  Created by Clement_Gu on 16/4/11.
//  Copyright © 2016年 小白. All rights reserved.
//

#import "XBTabBar.h"

@interface XBTabBar()
@property (nonatomic,weak) UIButton *plusButton;
@end
@implementation XBTabBar
//懒加载创建唯一的button
-(UIButton *)plusButton
{
    if (_plusButton == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [btn sizeToFit];
        _plusButton = btn;
        [self addSubview:_plusButton];
    }
    return _plusButton;
    
}




//调整子控件的位置
-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置UItabbarButton的frame
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    CGFloat X = width/(self.items.count + 1);
    CGFloat Y = 0;
    int i = 0;

    //调整系统自带的按钮的位置
    for (UIView *tabBarButton in self.subviews)
    {
    
        //判断是否UItabBarItem
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            if (i == 2) {
                i = 3;
            }
            
            CGFloat btnX = i*X;
        
            
            tabBarButton.frame = CGRectMake(btnX, Y, width/(self.items.count + 1), height);
            
            i++;
        }
    
    
    }
    //设置中间按钮
    self.plusButton.center = CGPointMake(width*0.5, height*0.5);
    
    
        
}


@end
